import { Suspense } from 'react'
import { Switch, Route, Redirect,withRouter } from 'react-router-dom'

function RouterView(props) {
    const { routes } = props
    return <Suspense fallback="<p>嘿嘿嘿，等着吧~~~</p>">
        <Switch>
            {
                routes.map((item, index) => {
                    if (item.component) {
                        return <Route key={index} exact={item.exact} path={item.path} render={
                            ()=>{
                                const userinfo = JSON.parse(localStorage.getItem("loginData")||'{}')
                                if(item.requeireLogin){ //需要登录
                                    if(!userinfo.token){ //没有token 
                                        return <Route key={index} exact={item.exact} path={item.path}>
                                             <Redirect to="/login" /> {/* 跳转到登录 */}
                                        </Route>
                                    }
                                }
                                var Component = withRouter(item.component)
                                return <Component/>
                            }
                        } />
                    } else {
                        return <Route key={index} exact={item.exact} path={item.path}>
                            <Redirect to={item.redirect} />
                        </Route>
                    }


                })
            }
        </Switch>
    </Suspense>
}

export default RouterView
